<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import api from '/@/api/email/index';
import { LoadingSvg } from '/@/utils/loading';
import Edit from './components/edit.vue'

defineOptions({ name: 'CardApplyIndex' })

const tableData = ref([]);
const tableHeight = ref(500);
const queryForm = ref<any>({
  pis: '',
  state: undefined,
});

const loading = ref(false);
const stateMap = ['未刷卡', '刷卡中', '已刷卡']

const show = ref(false)
const editRef = ref()
const pagination = ref({
  page: 1,
  size: 10,
  total: 0
});

const handleQuery = () => {
  pagination.value.page = 1;
  getList();
};

const handleReset = () => {
  queryForm.value = {};
  getList()
};

const create = () => {
  show.value = true
}

const getList = async () => {
  loading.value = true;
  const res = await api.cardApply.list({
    pageNum: pagination.value.page,
    pageSize: pagination.value.size,
    ...queryForm.value
  });
  loading.value = false;
  if (res.code === 200) {
    tableData.value = res.data;
    pagination.value.total = res.total;
  }
};

const edit = (row: any) => {
  show.value = true
  editRef.value.getInfo(row.id)
}

const del = (row: any) => {
  ElMessageBox.confirm(`确认删除吗?`, '提示', { cancelButtonText: '取消', confirmButtonText: '确认' }).then(async () => {
    const res = await api.cardApply.del(row.id);
    if (res.code === 200) {
      ElMessage({ type: 'success', message: '操作成功' });
      getList();
    }
  });
}

const handleCurrentChange = (page: number) => {
  pagination.value.page = page;
  getList();
};

onMounted(() => {
  getList();

  const setTableHeight = () => {
    tableHeight.value = window.innerHeight - 170 // 80 为顶部距离，根据实际情况设置
  }
  setTableHeight()
  window.addEventListener('resize', setTableHeight);
});
</script>
<template>
  <div class="main">
    <el-form class="btn_con" v-model="queryForm">
      <el-row :gutter="8">
        <el-col :span="4">
          <el-input v-model="queryForm.pis" placeholder="PI" class="w-100" />
        </el-col>
        <el-col :span="4">
          <el-select v-model="queryForm.state" placeholder="请选择状态" clearable class="w-100">
            <el-option v-for="(value, key) in stateMap" :label="value" :value="key" :key="key" />
          </el-select>
        </el-col>
        <el-col :span="11"></el-col>
        <el-col :span="5" class="add_btn">
          <el-button @click="handleQuery" type="primary">搜索</el-button>
          <el-button @click="handleReset" type="primary">重置</el-button>
          <el-button @click="create" type="success">填写申请</el-button>
        </el-col>
      </el-row>
    </el-form>
    <div>
      <el-table :data="tableData" border style="width: 100%" :height="tableHeight" v-loading="loading"
        element-loading-svg-view-box="0, 0, 1024, 1024" :element-loading-svg="LoadingSvg">
        <el-table-column prop="voucherUrl" label="付款截图">
          <template #default="{ row }">
            <el-space v-if="row.voucherUrl">
              <el-image v-for="(item, index) in row.voucherUrl.split(',')" :key="index" :src="item" fit="cover"
                :zoom-rate="1.2" :max-scale="7" :min-scale="0.2" :initial-index="index"
                :preview-src-list="row.voucherUrl.split(',')" preview-teleported style="width: 110px; height: 60px;" />
            </el-space>
          </template>
        </el-table-column>
        <el-table-column prop="salesName" label="业务员" />
        <el-table-column prop="companyCode" label="公司" />
        <el-table-column prop="pis" label="PI" />
        <el-table-column prop="amount" label="刷卡金额" />
        <el-table-column prop="cardNumber" label="卡号" />
        <el-table-column prop="expires" label="过期日期" />
        <el-table-column prop="code" label="安全码" />
        <el-table-column prop="state" label="状态">
          <template #default="{ row }">
            <el-tag
              :type="['info', 'success', 'primary'][row.state]"
              effect="Light"
            >
              {{ stateMap[row.state] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" />
        <el-table-column prop="createTime" label="申请时间" />
        <el-table-column label="操作" width="140" fixed="right">
          <template #default="{ row }">
            <el-button plain type="primary" size="small" @click="edit(row)">修改</el-button>
            <el-button plain type="primary" size="small" @click="del(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination_con">
        <el-pagination @current-change="handleCurrentChange" :current-page="pagination.page" :total="pagination.total"
          small background layout="prev, pager, next" />
      </div>
    </div>

    <Edit ref="editRef" v-model="show" @refresh="getList" />
  </div>
</template>
<style lang="scss" scoped>
.main {
  width: 100%;
  height: 100vh;
  padding: 20px;
}

.pagination_con {
  display: flex;
  justify-content: end;
  padding: 10px;
  background: #fff;
}

.btn_con {
  padding: 7px 10px;
}

.add_btn {
  display: flex;
  justify-content: right;
  align-items: center;
}
.w-100 {
  width: 100%;
}
</style>